package com.bw.journal.list

import android.view.LayoutInflater
import androidx.activity.viewModels
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewbinding.ViewBinding
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.library_mvi.ui.BaseActivity
import com.bawei.library_mvi.ui.BaseMviActivity
import com.bawei.module_journal.R
import com.bawei.module_journal.databinding.ActivityJournalBinding
import com.bawei.module_journal.list.JournalIntent
import com.bawei.module_journal.list.JournalState
import com.bawei.module_journal.list.entity.RequestEntity
import com.bw.journal.insert.JournalInsertViewModel

import kotlinx.coroutines.launch

@Route(path = "/journal/JournalActivity")
class JournalActivity : BaseMviActivity<ActivityJournalBinding>(){
    private lateinit var viewModel:JournalListViewModel
    private val journalAdapter : JournalAdapter by lazy { JournalAdapter() }



    override fun initView() {
        viewDataBinding.journalListPage.titleTv.text = "工作汇报"
        viewDataBinding.journalListPage.backImg.setOnClickListener { finish() }
        viewDataBinding.journalAddImg.setOnClickListener {
            ARouter.getInstance().build("/journal/JournalInsertActivity").navigation()
        }
        val manager = LinearLayoutManager(this)
        manager.orientation = LinearLayoutManager.VERTICAL
        viewDataBinding.journalListRv.layoutManager = manager
        viewDataBinding.journalListRv.adapter = journalAdapter
    }

    override fun onRestart() {
        super.onRestart()
        initData()
    }

    override fun handleState() {
        lifecycleScope.launch {
            viewModel.states.collect{
                when(it){
                    is JournalState.LoadJournalSuccessState ->{
                        val list = it.list
                        journalAdapter.setNewInstance(list)
                    }
                    is JournalState.Init ->{}
                }
            }
        }

    }

    override fun initData() {
        lifecycleScope.launch{
            viewModel.intents.send(JournalIntent.LoadJournalIntent(RequestEntity(1,"1")))
        }
    }



    override fun getLayout(): Int {
       return R.layout.activity_journal
    }

    override fun generateViewModel() {
        viewModel = ViewModelProvider(this).get(JournalListViewModel::class.java)
    }


}